<html><head><style>
  .str { color: #0d3; }
  .comm { color: #888; }
  .prep { color: #00f; }
  .ptr { color: #b80; }
  .dir { color: #08e; }
  .name { color: #da0; }
  .con { color: #f00; }
</style></head><body>

<p>Example of syntax-highlighted comun program (highlighted with a comun program in example directory):</p>

<pre>
<span class='comm'># small comun library of generally useful functions</span>
<span class='comm'># by drummyfish, released under CC0 1.0, public domain</span>

<span class='comm'># TODO: exp? strfind</span>

<span class='comm'># internal library pointers</span>
<span class='dir'>~_mLibPtrA:0</span>   <span class='comm'># tmp pointer</span>
<span class='dir'>~_mLibPtrB:0</span>   <span class='comm'># tmp pointer</span>
<span class='dir'>~_mLibTmpMem:3</span> <span class='comm'># tiny tmp memory</span>
<span class='dir'>~_mLibVarA:0</span>   <span class='comm'># tmp variable, 2nd cell of tmp memory</span>
<span class='dir'>~_mLibVarB:0</span>   <span class='comm'># tmp variable, 3rd cell of tmp memory</span>

<span class='ptr'>$_mLibTmpMem&gt;_mLibVarA</span>
<span class='ptr'>$&gt;_mLibVarA</span>
<span class='ptr'>$_mLibVarA&gt;_mLibVarB</span>
<span class='ptr'>$&gt;_mLibVarB</span>

<span class='comm'># ========== GENERAL ===========================================================</span>

<span class='name'>abs:</span> <span class='ptr'>$0</span> -1</span> &lt;&lt;</span> <span class='con'>?</span> -1</span> *</span> <span class='con'>.</span> <span class='con'>.</span> <span class='comm'># Absolute value.</span>
<span class='name'>min:</span> &gt;'</span> <span class='con'>?</span> &gt;&lt;</span> <span class='con'>.</span> ^</span> <span class='con'>.</span>       <span class='comm'># Minimum of two values.</span>
<span class='name'>max:</span> &lt;'</span> <span class='con'>?</span> &gt;&lt;</span> <span class='con'>.</span> ^</span> <span class='con'>.</span>       <span class='comm'># Maximum of two vlaues.</span>
<span class='name'>minS:</span> &gt;&gt;'</span> <span class='con'>?</span> &gt;&lt;</span> <span class='con'>.</span> ^</span> <span class='con'>.</span>     <span class='comm'># Minimum of two signed values.</span>
<span class='name'>maxS:</span> &lt;&lt;'</span> <span class='con'>?</span> &gt;&lt;</span> <span class='con'>.</span> ^</span> <span class='con'>.</span>     <span class='comm'># Maximum of two signed values.</span>
<span class='name'>min3:</span> <span class='name'>min</span> <span class='name'>min</span> <span class='con'>.</span>          <span class='comm'># Minimum of three values.</span>
<span class='name'>max3:</span> <span class='name'>max</span> <span class='name'>max</span> <span class='con'>.</span>          <span class='comm'># Maximum of three values.</span>

<span class='comm'># Interpolates from $3 to $2 by a factor of $1/$0.</span>
<span class='name'>lerp:</span>
  <span class='ptr'>$3</span> <span class='ptr'>$3</span> &gt;</span> <span class='con'>?</span> <span class='comm'># if going from higher to lower, swap</span>
    <span class='ptr'>$3</span> <span class='ptr'>$3</span> <span class='ptr'>$:5</span> <span class='ptr'>$:3</span>
    <span class='ptr'>$0</span> <span class='ptr'>$2</span> -</span> <span class='ptr'>$:2</span>
  <span class='con'>.</span>

  &gt;&lt;</span> <span class='ptr'>$2</span> <span class='ptr'>$4</span> -</span> *</span> &gt;&lt;</span> /</span> &gt;&lt;</span> ^</span> +</span>
<span class='con'>.</span>

<span class='comm'># Hashes input number to a 16 bit value, can also be used as a pseudorandom</span>
<span class='comm'># generator.</span>
<span class='name'>hash:</span>
  <span class='ptr'>$0</span> +b010000000</span> *</span> +</span>  +xffff</span> %</span>
  <span class='ptr'>$0</span> +b100000000</span> /</span> |!</span> +xffff</span> %</span>
  <span class='ptr'>$0</span> +b000001000</span> *</span> +</span>  +xffff</span> %</span>
  <span class='ptr'>$0</span> +b000000100</span> /</span> |!</span> +xffff</span> %</span>
  <span class='ptr'>$0</span> +b000010000</span> *</span> +</span>  +xffff</span> %</span>
  <span class='ptr'>$0</span> +b100000000</span> /</span> |!</span> +xffff</span> %</span>
<span class='con'>.</span>

<span class='comm'># ========== STRINGS ===========================================================</span>
<span class='comm'># String is a sequence of ASCII character codes, considered starting stack top</span>
<span class='comm'># and continuing downwards, ending with value 0.</span>

<span class='comm'># Reads newline-terminated string from input, pushes it as a zero terminated</span>
<span class='comm'># string in reversed order, without the newline.</span>
<span class='name'>strRead:</span>
  0</span>

  <span class='con'>@@</span>
    &lt;-</span>

    <span class='ptr'>$0</span> 10</span> =</span> <span class='con'>?</span>
      ^</span> !@</span>
    <span class='con'>.</span>
  <span class='con'>.</span>

  <span class='name'>strReverse</span>
<span class='con'>.</span>

<span class='comm'># Prints values from memory stack up to stack top.</span>
<span class='name'>stackPrint:</span>
  <span class='ptr'>$$</span> <span class='name'>arrayPrint</span>
<span class='con'>.</span>

<span class='comm'># Computes string length.</span>
<span class='name'>strLen:</span>
  <span class='ptr'>$0&gt;_mLibPtrA</span>
  0</span>

  <span class='con'>@@</span>
    <span class='ptr'>$_mLibPtrA</span> 0</span> =</span> <span class='con'>?</span>
      !@</span>
    <span class='con'>.</span>

    ++</span>
    <span class='ptr'>$&lt;_mLibPtrA</span>
  <span class='con'>.</span>
<span class='con'>.</span>

<span class='comm'># Expects two strings which won't be popped, pushes 1 if they're equal, else 0.</span>
<span class='name'>strEq:</span>
  <span class='ptr'>$0&gt;_mLibPtrA</span>
  <span class='ptr'>$0&gt;_mLibPtrB</span>

  <span class='con'>@@</span>
    <span class='ptr'>$_mLibPtrB</span> 0</span> =</span> <span class='con'>?</span>
      !@</span>
    <span class='con'>.</span>

    <span class='ptr'>$&lt;_mLibPtrB</span>
  <span class='con'>.</span>

  <span class='ptr'>$&lt;_mLibPtrB</span>

  <span class='con'>@@</span>
    <span class='ptr'>$_mLibPtrA</span> <span class='ptr'>$_mLibPtrB</span> !=</span> <span class='con'>?</span>
      0</span> !.</span>
    <span class='con'>.</span>

    <span class='ptr'>$_mLibPtrA</span> 0</span> =</span> <span class='con'>?</span>
      1</span> !.</span>
    <span class='con'>.</span>

    <span class='ptr'>$&lt;_mLibPtrA</span>
    <span class='ptr'>$&lt;_mLibPtrB</span>
  <span class='con'>.</span>
<span class='con'>.</span>

<span class='comm'># Left-pads a string to length $1 with character $0.</span>
<span class='name'>strPad:</span>
  <span class='ptr'>$:_mLibVarA</span>
  <span class='ptr'>$:_mLibVarB</span>

  <span class='name'>strLen</span> <span class='ptr'>$_mLibVarB</span> &gt;&lt;</span> -</span>

  <span class='con'>@@</span>
    <span class='ptr'>$0</span> 0</span> &lt;&lt;=</span> <span class='con'>?</span>
      ^</span> !@</span>
    <span class='con'>.</span>

    <span class='ptr'>$_mLibVarA</span> &gt;&lt;</span>
    --</span>
  <span class='con'>.</span>
<span class='con'>.</span>

<span class='comm'># Reverses a zero-terminated string.</span>
<span class='name'>strReverse:</span>
  <span class='name'>strLen</span> <span class='name'>arrayReverse</span> ^</span>
<span class='con'>.</span>

<span class='comm'># ========== MATH ==============================================================</span>

<span class='comm'># Power: pops 2, pushes $1 raised to $0.</span>
<span class='name'>pow:</span>
  1</span> &gt;&lt;</span>

  <span class='con'>@'</span>
    &gt;&lt;</span> <span class='ptr'>$2</span> *</span> &gt;&lt;</span>
    --</span>
  <span class='con'>.</span>
 
  ^</span> &gt;&lt;</span> ^</span> 
<span class='con'>.</span>

<span class='comm'># Square roots a number using binary search.</span>
<span class='name'>sqrt:</span>
  0</span> <span class='ptr'>$1</span> 2</span> /</span>  <span class='comm'># stack: x lower upper</span>

  <span class='con'>@@</span>
    <span class='ptr'>$0</span> <span class='ptr'>$2</span> -</span> 1</span> &lt;=</span> <span class='con'>?</span>
      !@</span>
    <span class='con'>.</span>

    <span class='ptr'>$1</span> <span class='ptr'>$1</span> +</span> 2</span> /</span>
    <span class='ptr'>$0</span> <span class='ptr'>$0</span> *</span>    <span class='comm'># stack: x lower upper middle middle^2</span>

    <span class='ptr'>$4</span> &gt;</span> <span class='con'>?</span>
      <span class='ptr'>$:1</span>
      <span class='con'>;</span>
      <span class='ptr'>$:2</span>
    <span class='con'>.</span>
  <span class='con'>.</span>

  <span class='ptr'>$0</span> <span class='ptr'>$0</span> *</span> <span class='ptr'>$3</span> &lt;=</span> <span class='con'>?</span>  
    &gt;&lt;</span>
  <span class='con'>.</span>

  ^</span> &gt;&lt;</span> ^</span>
<span class='con'>.</span>

<span class='comm'># Computes integer logarithm of $0 with base $1.</span>
<span class='name'>log:</span>
  0</span> <span class='comm'># result</span>
  1</span> <span class='comm'># exponentiated value</span>
  1</span> <span class='comm'># previous exponentiated value</span>

  <span class='con'>@@</span>
    <span class='ptr'>$1</span> <span class='ptr'>$4</span> =</span> <span class='con'>?</span> <span class='comm'># equality has to be checked, considering overflow may happen next</span>
      !@</span>
    <span class='con'>.</span>

    <span class='ptr'>$2</span> ++</span> <span class='ptr'>$:3</span>    <span class='comm'># compute next power</span>
    <span class='ptr'>$1</span> <span class='ptr'>$5</span> *</span> <span class='ptr'>$:2</span>

    <span class='ptr'>$1</span> <span class='ptr'>$4</span> &gt;</span> <span class='ptr'>$2</span> <span class='ptr'>$2</span> &lt;=</span> ||</span> <span class='con'>?</span> <span class='comm'># new power &gt; x or new &lt;= previous (overflow)?</span>
      <span class='ptr'>$2</span> --</span> <span class='ptr'>$:3</span>
      !@</span>      
    <span class='con'>.</span>

    <span class='ptr'>$1</span> <span class='ptr'>$:1</span>       <span class='comm'># current -&gt; previous</span>
  <span class='con'>.</span>

  ^</span> ^</span> &gt;&lt;</span> ^</span> &gt;&lt;</span> ^</span>
<span class='con'>.</span>

<span class='comm'># Computes 8bit sine function approximation with quadratic curve, value 256 on</span>
<span class='comm'># input represents the whole period (2 * pi), output value is between 0 and 255.</span>
<span class='name'>sin8:</span>
  <span class='ptr'>$0</span> 256</span> %</span> 64</span> /</span> <span class='comm'># quadrant</span>
  &gt;&lt;</span> 64</span> %</span>

  <span class='ptr'>$1</span> 2</span> %</span> 1</span> =</span> <span class='con'>?</span>
    63</span> &gt;&lt;</span> -</span>
  <span class='con'>.</span>

  63</span> -</span> <span class='ptr'>$0</span> *</span> 32</span> /</span>

  &gt;&lt;</span> 1</span> &lt;=</span> <span class='con'>?</span>
    255</span> &gt;&lt;</span> -</span>
  <span class='con'>.</span>
<span class='con'>.</span>

<span class='comm'># Computes 8bit cosine function approximation.</span>
<span class='name'>cos8:</span>
  64</span> +</span> <span class='name'>sin8</span>
<span class='con'>.</span>

<span class='comm'># Computes factorial recursively.</span>
<span class='name'>fact:</span>
  <span class='con'>?'</span>
    <span class='ptr'>$0</span> --</span> <span class='name'>fact</span> *</span>
  <span class='con'>;</span>
    ^</span> 1</span>
  <span class='con'>.</span>
<span class='con'>.</span>

<span class='comm'># Checkes if number is a prime.</span>
<span class='name'>isPrime:</span>
  <span class='ptr'>$0</span> 1</span> &lt;=</span>
  <span class='con'>?</span>
    ^</span> 0</span> <span class='comm'># 0 and 1 are not primes</span>
  <span class='con'>;</span>
    <span class='ptr'>$0</span> 2</span> /</span>

    <span class='con'>@@</span>
      <span class='ptr'>$0</span> 1</span> &lt;=</span>
      <span class='con'>?</span>
        ^</span> ^</span> 1</span> !.</span>
      <span class='con'>.</span>

      <span class='ptr'>$1</span> <span class='ptr'>$1</span> %</span> 0</span> =</span>
      <span class='con'>?</span>
        ^</span> ^</span> 0</span> !.</span>
      <span class='con'>.</span>

      --</span>
    <span class='con'>.</span>
  <span class='con'>.</span>
<span class='con'>.</span>

<span class='comm'># ========== NUMBER PRINTING AND CONVERSION TO/FROM STRINGS ====================</span>

<span class='comm'># Internal function, converts number to base passed in variable A.</span>
<span class='name'>_numToStrBase:</span>
  0</span> &gt;&lt;</span>
  <span class='con'>@@</span>
    <span class='ptr'>$0</span> <span class='ptr'>$_mLibVarA</span> %</span> 

    <span class='ptr'>$0</span> 10</span> &lt;</span> <span class='con'>?</span> <span class='str'>"0"</span> <span class='con'>;</span> <span class='str'>"A"</span> 10</span> -</span> <span class='con'>.</span>
    +</span> &gt;&lt;</span>
    <span class='ptr'>$_mLibVarA</span> /</span>

    <span class='ptr'>$0</span> 0</span> =</span> <span class='con'>?</span>
      !@</span>
    <span class='con'>.</span>
  <span class='con'>.</span>
  ^</span>
<span class='con'>.</span>

<span class='name'>numToStrBin:</span> 2</span> <span class='ptr'>$:_mLibVarA</span> <span class='name'>_numToStrBase</span> <span class='con'>.</span>
<span class='name'>numToStrHex:</span> 16</span> <span class='ptr'>$:_mLibVarA</span> <span class='name'>_numToStrBase</span> <span class='con'>.</span>
<span class='name'>numToStr:</span> 10</span> <span class='ptr'>$:_mLibVarA</span> <span class='name'>_numToStrBase</span> <span class='con'>.</span>

<span class='comm'># Converts a signed number to zero-terminated string.</span>
<span class='name'>numToStrS:</span>
  <span class='ptr'>$0</span> 0</span> &lt;&lt;</span>
  <span class='con'>?</span>
    -1</span> &gt;&lt;</span> -</span> 1</span> +</span>
    <span class='name'>numToStr</span>
    <span class='str'>"-"</span>
  <span class='con'>;</span>
    <span class='name'>numToStr</span>
  <span class='con'>.</span>
<span class='con'>.</span>

<span class='name'>numPrint:</span> <span class='name'>numToStr</span> --&gt;</span> <span class='con'>.</span>
<span class='name'>numPrintS:</span> <span class='name'>numToStrS</span> --&gt;</span> <span class='con'>.</span>
<span class='name'>numPrintHex:</span> <span class='name'>numToStrHex</span> --&gt;</span> <span class='con'>.</span>
<span class='name'>numPrintBin:</span> <span class='name'>numToStrBin</span> --&gt;</span> <span class='con'>.</span>

<span class='comm'># Converts number from decadic string (zero terminated reverse pushed on stack),</span>
<span class='comm'># negative numbers are also supported, validity is not checked.</span>
<span class='name'>numFromStr:</span>
  <span class='ptr'>$0</span> <span class='str'>"-"</span> =</span> <span class='con'>?</span>
    ^</span> <span class='name'>numFromStr</span> -1</span> *</span>
  <span class='con'>;</span>
    0</span>

    <span class='con'>@@</span>
      <span class='ptr'>$1</span> 0</span> =</span> <span class='con'>?</span>
        !@</span>
      <span class='con'>.</span>

      10</span> *</span>

      <span class='str'>"0"</span> -</span> +</span>
    <span class='con'>.</span>

    &gt;&lt;</span> ^</span>
  <span class='con'>.</span>
<span class='con'>.</span>

<span class='comm'># ========== ARRAYS ============================================================</span>
<span class='comm'># Array here means N valus on the stack and the value N pushed afterwards.</span>
<span class='comm'># Functions working with arrays normally leave this unchanged without popping,</span>
<span class='comm'># e.g. sorting an array will just change orders of the items in the array.</span>

<span class='comm'># Prints out given array (from lowest memory address towards stack top).</span>
<span class='name'>arrayPrint:</span>
  <span class='ptr'>$0&gt;_mLibPtrA</span>
  <span class='ptr'>$:_mLibVarA'</span>

  <span class='con'>@@</span> <span class='comm'># move pointer A to bottom of array</span>
    <span class='ptr'>$_mLibVarA</span>

    <span class='con'>?'</span>
      --</span> <span class='ptr'>$:_mLibVarA</span>
      <span class='ptr'>$&lt;_mLibPtrA</span>
    <span class='con'>;</span>
      ^</span> !@</span>
    <span class='con'>.</span>
  <span class='con'>.</span>

  <span class='con'>@@</span> <span class='comm'># now go up and print</span>
    <span class='ptr'>$_mLibPtrA=0</span> 0</span> =</span> <span class='con'>?</span>
      !@</span>
    <span class='con'>.</span>

    <span class='ptr'>$_mLibPtrA</span> <span class='name'>printNum</span> <span class='str'>" "</span> -&gt;</span>
    <span class='ptr'>$&gt;_mLibPtrA</span>
  <span class='con'>.</span>
<span class='con'>.</span>

<span class='comm'># Reverses items in given array.</span>
<span class='name'>arrayReverse:</span>
  <span class='ptr'>$0&gt;_mLibPtrA</span>
  <span class='ptr'>$:_mLibVarA'</span>

  <span class='con'>@@</span>
    <span class='ptr'>$_mLibVarA</span>

    <span class='con'>?'</span>
      --</span> <span class='ptr'>$:_mLibVarA</span>
      <span class='ptr'>$&lt;_mLibPtrA</span>
    <span class='con'>;</span>
      ^</span> !@</span>
    <span class='con'>.</span>
  <span class='con'>.</span>

  <span class='ptr'>$0&gt;_mLibPtrB</span>
  <span class='ptr'>$&lt;_mLibPtrB</span>

  <span class='con'>@@</span>
    <span class='ptr'>$_mLibPtrA=_mLibPtrB</span> 2</span> !=</span> <span class='con'>?</span>
      !.</span>
    <span class='con'>.</span>

    <span class='ptr'>$_mLibPtrA</span> <span class='ptr'>$_mLibPtrB</span>
    <span class='ptr'>$:_mLibPtrA</span> <span class='ptr'>$:_mLibPtrB</span>

    <span class='ptr'>$&gt;_mLibPtrA</span>
    <span class='ptr'>$&lt;_mLibPtrB</span>
  <span class='con'>.</span>
<span class='con'>.</span>

<span class='comm'># Sorts an array in ascending order (towards stack top) with bubble sort.</span>
<span class='name'>arraySort:</span>
  <span class='ptr'>$0</span> --</span>

  <span class='con'>@'</span>
    <span class='ptr'>$0&gt;_mLibPtrA</span>
    <span class='ptr'>$&lt;_mLibPtrA</span>
    <span class='ptr'>$&lt;_mLibPtrA</span>

    <span class='ptr'>$0</span>
    <span class='con'>@'</span>
      <span class='ptr'>$_mLibPtrA</span> <span class='ptr'>$&lt;_mLibPtrA</span> <span class='ptr'>$_mLibPtrA</span>

      &lt;'</span> <span class='con'>?</span>
        &gt;&lt;</span>
        <span class='ptr'>$:_mLibPtrA</span> <span class='ptr'>$&gt;_mLibPtrA</span> <span class='ptr'>$:_mLibPtrA</span> <span class='ptr'>$&lt;_mLibPtrA</span>
      <span class='con'>;</span>
        ^</span> ^</span>
      <span class='con'>.</span>

      --</span>
    <span class='con'>.</span>

    ^</span>
    --</span>
  <span class='con'>.</span>
  ^</span>
<span class='con'>.</span>

<span class='comm'># Sorts an array in descending order.</span>
<span class='name'>arraySortDesc:</span>
  <span class='name'>arraySort</span> <span class='name'>arrayReverse</span>
<span class='con'>.</span>

</pre></body></html>
